function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function subTen(value) {
  return value < 10 ? ('0' + value) : value;
}
// 判断是否为闰年 true则为闰年
function isLeapYear(year) {
  return (year % 400 === 0) || (year % 100 !== 0 && year % 4 === 0);
};
// 获取多少天后的日期
function Fungetdate(count, time) {
  var dd = time ? new Date(time) : new Date();
  // console.log('dd: ',dd)
  dd.setDate(dd.getDate() + count);//获取p_count天后的日期
  var y = dd.getFullYear();
  var m = dd.getMonth() + 1;//获取当前月份的日期
  var d = dd.getDate();
  // return y + "-" + m + "-" + d;
  //位数对齐两位
  if(m.toString().length === 1) {
    m = "0" + m;
  }
  if(d.toString().length === 1) {
    d = "0" + d;
  }
  return [y, m, d]
}
// 获取本周日期
function getWeekday(next = 0) {
  const nextDataArr = Fungetdate(next * 7)
  const year = nextDataArr[0]
  const day = nextDataArr[2]
  const month = nextDataArr[1] - 1
  let weekStr = '日一二三四五六';
  // const weekArr = weekStr.split('').map(item => '星期' + item);
  const weekArr = weekStr.split('')
  let week = []
  const monthNum = [
    31, null, 31, 30,
    31, 30, 31, 31,
    30, 31, 30, 31
  ];
  // 上月天数
  let lastCount = () => {
    if (month === 0) {
      return 31;
    } else {
      return monthNum[month - 1] || (isLeapYear(year) ? 29 : 28);
    }
  }
  // 获取周几
  const getWeekday = (year, month, day) => {
    let date = new Date(year, month, day);
    return date.getDay();
  };
  let _year = year;
  for (let i = 0; i < 7; i++) {
    let monthText = (day - i) > 0 ? month : (month - 1) >= 0 ? (month - 1) : 11
    let dayText = (day - i) > 0 ? day - i : lastCount() - (i - day);
    let weekText = getWeekday(_year, monthText, dayText)
    week.unshift({ day: dayText, week: weekArr[weekText], time: `${_year}-${monthText + 1}-${dayText}`, month: monthText + 1 })
    if(monthText === 0 && dayText === 1) {
      _year = year - 1
    }
  }
  return week
}
module.exports = {
  formatTime: formatTime,
  subTen,
  getWeekday,
  Fungetdate
}